<?php
namespace addon\cms\controller;

use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use tpfcore\helpers\Json;
use think\Request;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 */
class Fields extends AddonAdminBase
{
    public function index()
    {
        empty($this->param['form_id']) && $this->redirect(url('cms/CustomForm/index'));
        $list =Core::loadAddonModel("CustomForm")->getCustomForm(["where"=>["id"=>$this->param['form_id']],"field"=>"id,fields"]);
        $this->addonTemplate("index",[
            "list"=>$list
        ]);
    }
    public function add()
    {

        IS_POST && $this->jump(Core::loadAddonModel($this->name)->saveFields($this->post));

        if(empty($this->param['form_id'])){
            $this->redirect(url('cms/CustomForm/index'));
        }

        $form_id = $this->param['form_id'];
        $listCustomForm = Core::loadAddonModel("CustomForm")->getCustomForm([
            "where"=>["id"=>$form_id]
        ]);
        if(empty($listCustomForm)){
            $this->redirect(url('cms/CustomForm/index'));
        }

        $this->addonTemplate("add",[
            "form_id"=>$this->param['form_id'],
            "list"=>empty($this->param['id'])?[]:Core::loadAddonModel($this->name)->getFieldList(["where"=>["id"=>$this->param['id']]])
        ]);
    }

    public function edit(){
        IS_POST && $this->jump(Core::loadAddonModel($this->name)->saveFields($this->post));

        if(empty($this->param['form_id'])){
            $this->redirect(url('cms/CustomForm/index'));
        }

        $form_id = $this->param['form_id'];
        $listCustomForm = Core::loadAddonModel("CustomForm")->getCustomForm([
            "where"=>["id"=>$form_id]
        ]);
        if(empty($listCustomForm)){
            $this->redirect(url('cms/CustomForm/index'));
        }

        //print_r($listCustomForm[0]['fields'][$this->param['id']]);die;

        $this->addonTemplate("edit",[
            "form_id"=>$this->param['form_id'],
            "id"=>$this->param['id'],
            "list"=>$listCustomForm[0]['fields'][$this->param['id']]
        ]);
    }

    public function del()
    {
        $this->jump(Core::loadAddonModel($this->name)->delFields($this->param));
    }
    public function get_extends_params(){

        if(!empty($this->param['form_id'])){
            $form_id = $this->param['form_id'];
            $listCustomForm = Core::loadAddonModel("CustomForm")->getCustomForm([
                "where"=>["id"=>$form_id]
            ]);

            $fields = $listCustomForm[0]['fields'][$this->param['id']];

        }

        return $this->addonTemplate("extends",[
            "type"=>empty($this->param['type'])?$fields['type']:$this->param['type'],
            "list"=>empty($listCustomForm)?[]:$listCustomForm[0]['fields'][$this->param['id']]
        ]);
    }
} 
